/**
 * 返回一个 promise 对象，async await 接收
 */
export default (
	// 上传地址
	url = '',
	count = 1,
	name = 'file'
) => {
	const choose = new Promise((resolve, reject) => {
		uni.chooseImage({
			count,
			success(res) {
				resolve([null, res.tempFilePaths])
			},
			fail(err) {
				resolve([err])
			}
		})
	})
	return choose.then(([错误, 图片列表]) => {
		if (错误 !== null) return console.error(错误);
		if (图片列表.length == 1) {
			return new Promise((resolve1, reject1) => {
				uni.uploadFile({
					url,
					filePath: 图片列表[0],
					name,
					success(rej) {
						let url = ''
						for (let key in JSON.parse(rej.data)) {
							if (JSON.parse(rej.data)[key].toString().includes('/')) {
								url = JSON.parse(rej.data)[key]
								break
							}
						}
						resolve1([url])
					},
					fail(err) {
						reject1(err)
					}
				})
			})
		} else {
			const proList = 图片列表.map(item => {
				return new Promise((resolve2, reject2) => {
					uni.uploadFile({
						url: url,
						filePath: item,
						name,
						success(rej) {
							resolve2(JSON.parse(rej.data))
						},
						fail(err) {
							reject2(err)
						}
					})
				})
			})
			return Promise.all(proList).then(resolve3 => {
				const list = resolve3.map(item => {
					for (let key in item) {
						if (item[key].toString().includes('/')) {
							return item[key]
							break
						}
					}
				})
				return list
			}).catch(reject3 => {
				return reject3
			})
		}
	})
}
